package com.groupbaby.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.groupbaby.ydd.common.BusinessException;
import com.groupbaby.ydd.common.GetPage;
import com.groupbaby.ydd.common.ResultData;
import com.groupbaby.ydd.entity.YddLink;
import com.groupbaby.ydd.entity.YddUser;
import com.groupbaby.ydd.service.IYddUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@Slf4j
@RequestMapping("/ydd_user")
public class YddUserController {

    @Autowired
    private IYddUserService yddUserService;

    /**
     * 分页查询
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String sex, String username, String mobile, String page, String limit){

        int linkPage = Integer.parseInt(page);
        int pageSize = Integer.parseInt(limit);

        Page<YddUser> pageInfo = new Page<>(linkPage, pageSize);
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        if (sex!="") {
            queryWrapper.like(YddUser::getSex, sex);
        }
        if (username!="") {
            queryWrapper.like(YddUser::getUsername, username);
        }
        if (mobile!="") {
            queryWrapper.eq(YddUser::getMobile, mobile);
        }


        Page<YddUser> userPage = yddUserService.page(pageInfo, queryWrapper);

        int count = yddUserService.count(queryWrapper);

        List<YddUser> records = userPage.getRecords();
        GetPage getPage = GetPage.init((long) count, (long) pageSize, (long) linkPage, records);
        return ResultData.ok("", getPage);
    }

    /**
     * 根据id查询单个用户信息 修改对话框的数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(String id){
        YddUser yddUser = yddUserService.getById(id);
        return ResultData.ok("", yddUser);
    }

    /**
     * 按条件修改
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddUser yddUser){
        if(yddUser == null){
            throw  new BusinessException("用户不存在, 修改失败");
        }

        String password = yddUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        yddUser.setPassword(password);
        yddUserService.updateById(yddUser);
        return ResultData.ok("","修改成功");
    }

    /**
     * 添加保存
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddUser yddUser) {
        String password = yddUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        yddUser.setPassword(password);
        String birthday = String.valueOf(LocalDateTime.now());
        yddUser.setBirthday(birthday);
        yddUserService.save(yddUser);
        return ResultData.ok("","添加成功");
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(String id){
        yddUserService.removeById(id);
        return ResultData.ok("","删除成功");
    }


}
